import MySQLdb as mdb
import config
import error

myerror = error.econsole()
conf = config.db

class db():
    def __init__(self,parent=None,limit=None,where=[None,None],groupby=None,orderby=None,dac=None,fieldname=None):
        self.limit = limit
        self.where = where
        self.groupby = groupby
        self.orderby = orderby
        self.dac = dac
        self.fieldname = fieldname
    
    def makeConn(self):
        try:
            self.conn = mdb.connect(conf.host, conf.username, conf.password, conf.database)
            #if self.conn == False:
            return self.conn
            #else:
            #    myerror.getError("Not Connection !\n")
            #    return True
        except mdb.Error, e:
            myerror.getError(str(e))
        except self.conn.DatabaseError, e:
            myerror.getError(str(e))
        except self.conn.DataError, e:
            myerror.getError(str(e)) 
         
    def select(self, myconn, table):
        if self.limit == None:
            self.limit = "99999999999999999"
        else:
            self.limit = str(self.limit)
        if self.where == [None,None]:
            self.where = ["''","''"]
        else:
            if self.where[1] is str(self.where[1]):
                self.where = [self.where[0], "'" + self.where[1] + "'"]
            else:
                self.where = self.where
        if self.orderby == None:
            self.orderby = "''"
        else:
            self.orderby = 'id'
        if self.dac == None:
            self.dac = 'desc'
        else:
            self.dac = self.dac
        
        self.cursor = myconn.cursor()
        if self.groupby == None:
            sql = "SELECT * FROM " + table + " WHERE " + self.where[0] + " = " + self.where[1] + " ORDER BY " + self.orderby + " " + self.dac + " LIMIT " + self.limit   
        else:
            sql = "SELECT * FROM " + table + "WHERE " + self.where[0] + " = " + self.where[1] + " GROUP BY " + self.groupby + " ORDER BY " + self.orderby + " " + + self.dac + " LIMIT " + self.limit + " " 
        
        #print "SQL = ", sql
        self.cursor.execute(sql)
        data = self.cursor.fetchall()
        return data
    
    def selectbyfield(self, myconn, table):
        if self.limit == None:
            self.limit = "99999999999999999"
        else:
            self.limit = str(self.limit)
        if self.where == [None,None]:
            self.where = ["''","''"]
        else:
            if self.where[1] is str(self.where[1]):
                self.where = [self.where[0], "'" + self.where[1] + "'"]
            else:
                self.where = self.where
        if self.orderby == None:
            self.orderby = "''"
        else:
            self.orderby = 'id'
        if self.dac == None:
            self.dac = 'desc'
        else:
            self.dac = self.dac
        if self.fieldname == [None]:
            myerror.getError("Please fill name of field !")
        else:
            self.fieldname = self.fieldname
            
        self.cursor = myconn.cursor()
        print "DATA = ", str(",").join(self.fieldname) 
        if self.groupby == None:
            sql = "SELECT " + str(",").join(self.fieldname) + " FROM " + table + " WHERE " + self.where[0] + " = " + self.where[1] + " ORDER BY " + self.orderby + " " + self.dac + " LIMIT " + self.limit   
        else:
            sql = "SELECT " + str(",").join(self.fieldname) + " FROM " + table + "WHERE " + self.where[0] + " = " + self.where[1] + " GROUP BY " + self.groupby + " ORDER BY " + self.orderby + " " + + self.dac + " LIMIT " + self.limit + " " 
    
        print "SQL = ", sql
        try:
            self.cursor.execute(sql)
            data = self.cursor.fetchall()
            return data
        except myconn.Error, e:
            #myerror.getError(str(e))
            data_e = traceback.format_exc()
            return ['error',str(data_e)]
        except myconn.DatabaseError, e:
            #myerror.getError(str(e))
            data_e = traceback.format_exc()
            return ['error',str(data_e)]
        except myconn.DataError, e:
            #myerror.getError(str(e)) 
            data_e = traceback.format_exc()
            return ['error',str(data_e)]
            
        
        